package com.galleryviewpager.b_wave;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.galleryviewpager.DensityUtils;
import com.galleryviewpager.R;

/**
 * 锯齿
 */
public class TestWaveView extends View {


    private int mWidth, mHeight;
    private Context mContext;
    private int mwaveCount;
    private int mWaveHeight;
    private int mWaveColor;

    private RectF rectF = new RectF();

    private int mWaveWidth;


    private Paint mPaint = new Paint();

    public TestWaveView(Context context) {
        this(context, null);
    }

    public TestWaveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestWaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.TestWaveView, defStyleAttr, 0);

        mwaveCount = typedArray.getInt(R.styleable.TestWaveView_waveCount, 20);
        mWaveHeight = (int) typedArray.getDimension(R.styleable.TestWaveView_waveHeight, DensityUtils.dip2px(mContext, 10));
        mWaveColor = typedArray.getColor(R.styleable.TestWaveView_android_color, Color.RED);

        typedArray.recycle();

        initPaint();
    }

    private void initPaint() {
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(4f);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
        } else {
            mWidth = DensityUtils.dip2px(mContext, 400);
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
        } else {
            mHeight = DensityUtils.dip2px(mContext, 300);
        }

        rectF.set(mWaveHeight, 0, mWidth - mWaveHeight, mHeight);
        setMeasuredDimension(mWidth, mHeight);

        mWaveWidth = mHeight / mwaveCount;
    }


    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawRect(rectF, mPaint);

        for (int i = 0; i < mwaveCount; i++) {
            //左边部分
            canvas.drawLine(mWaveHeight, mWaveWidth * i, 0, (float) (mWaveWidth * (i + 0.5)), mPaint);
            canvas.drawLine(0, (float) (mWaveWidth * ((i + 0.5))), mWaveHeight, mWaveWidth * (i + 1), mPaint);

            //右边部分
            int stopX = mWidth - mWaveHeight;
            canvas.drawLine(stopX, mWaveWidth * i, mWidth, (float) (mWaveWidth * (i + 0.5)), mPaint);
            canvas.drawLine(mWidth, (float) (mWaveWidth * (i + 0.5)), stopX, mWaveWidth * (i + 1), mPaint);
        }
    }
}
